package com.geekgo.runningutil.web;

import com.geekgo.runningutil.util.WordToHtmlUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author geekgo
 * @version 2016/12/30 15:25
 */
@Controller
public class IndexController {

    private final Logger LOG = LogManager.getLogger();

    @RequestMapping("/")
    public String index(){

        return "index";
    }

    /**
     * word2003
     * 把word中的图片保存到服务器
     * @param req
     * @param rep
     * @return
     */
    @RequestMapping("/word1")
    public String wordToHtml1(HttpServletRequest req, HttpServletResponse rep){
        String rootPath = req.getServletContext().getRealPath("")+"/WEB-INF/classes";
        LOG.info("rootPath:"+rootPath);
        String fileName="test.docx";
        String basePath = rootPath+"/static/file";
        String wordPath = basePath+"/sources";
        String wordImagesPath=basePath+"/images";
        String htmlPath=basePath+"/html";
        LOG.info("wordPath:"+wordPath);
        String htmlName="";
        try {
            htmlName = WordToHtmlUtil.word07ToHtml(wordPath,fileName,wordImagesPath,htmlPath);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/file/html/"+htmlName;
    }

    /**
     * word2003
     * word中的图片直接转化成base64显示出来，不保存到服务器
     * @param request
     * @param response
     */
    @RequestMapping("/word2")
    public void wordToHtml2(HttpServletRequest request,HttpServletResponse response)throws IOException{
        String wordContent="";
        try {
            wordContent = WordToHtmlUtil.getWord03HtmlContent("D:/test/word03","免费下载百度文库word文档.doc");
        } catch (Exception e) {
            e.printStackTrace();
        }
        response.setCharacterEncoding("UTF-8");
        response.getWriter().print(wordContent);
    }
}
